
//
// 120622 - AH - Created.
// 121015 - AH - (Midtun *) Changed (dummy methods) clear and release to
//               protected methods _clear and _release.
//

package org.soekrat.engine;

import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.IOException;

import org.soekrat.conversationevent.ConversationEvent;
import org.soekrat.engine.captor.AbstractCaptor;
import org.soekrat.engine.captor.Captor;
import org.soekrat.engine.captor.Captors;
import org.soekrat.engine.orb.Soekrat2Orb;
import org.soekrat.engine.visitor.Line;
import org.soekrat.engine.visitor.Lines;
import org.soekrat.environment.Orb;
import org.soekrat.environment.Sensor;
import org.soekrat.environment.event.SensorEvent;

/**
 * <p>
 *   {@link Captor} that produce...
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class LineCaptor extends AbstractCaptor 
{
  protected void _clear()
  {
    // TODO Auto-generated method stub
    
  }

  protected void _release(Orb orb)
  {
    // TODO Auto-generated method stub  
  }

  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public LineCaptor(Captors captors)
  { 
    //super(captors); 
  }  
  
  // Specified in Captor.
  public String getSensedDataType(){ return "text"; }
  
  // Specified in Captor.
  public String getProducedDataType(){ return "lines"; }
  
  public void receive(ConversationEvent e)
  {
    if (e==null)
    {
      throw new NullPointerException("e");
    }
    
    SensorEvent se=(SensorEvent)e;
    
    Sensor sensor=(Sensor)se.getSource();
    
    if (sensor.getSensedDataType()!=this.getSensedDataType())
    {
      throw new IllegalArgumentException("not compatible data");
    }
    
    String txt=se.getData().toString();
    
    Lines lines=new Lines();
    
    CharArrayReader car=new CharArrayReader(txt.toCharArray());
    BufferedReader br=new BufferedReader(car);
    try
    {
      String line=br.readLine();
      while (line!=null)
      {
        line=line.trim();
        if (line.length()!=0)
        {
          lines.add(new Line(line));
        }
        line=br.readLine();
      }
      
      publish("", lines, se.getOrb());
    }
    catch (IOException iox)
    {
      // Never happens.
    }
  }
  
  /**
   * <p>
   *   Creates the
   *   {@link Lines} and put in on the
   *   {@link Orb} the old fashioned way.
   * </p>
   * @param txt Text to create 
   *            {@link Lines} from.
   * @param orb {@link Orb} to put 'captured' data on.
   */
  public static void createLinesToOrbS2(String txt, Orb orb)
  {
    Lines lines=new Lines();
    
    CharArrayReader car=new CharArrayReader(txt.toCharArray());
    BufferedReader br=new BufferedReader(car);
    try
    {
      String line=br.readLine();
      while (line!=null)
      {
        line=line.trim();
        if (line.length()!=0)
        {
          lines.add(new Line(line));
        }
        line=br.readLine();
      }
      
      orb.setAttribute(Soekrat2Orb.CHAT_INPUT, lines);   
    }
    catch (IOException iox)
    {
      // Never happens.
    } 
  }
 
}
